﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using ThangNM_LIB.FUNC;

namespace CSharp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtPath.Text = Server.MapPath("~/");

            hfAuto.Value = Security.MD5_Encrypt(OtherEx.getInfoRequest(2) + Folder_File.getFileNameByDateTime(ConstEx.Log_iType_NameFile_DateTime_C.Day, "") + "NmT@99!((@");
            ClientScriptManager cs = Page.ClientScript;
            if (!cs.IsClientScriptBlockRegistered(this.GetType(), "setIDFollow"))
            {
                String csText = "var idFollow = '" + txtIn.ClientID + "';";
                cs.RegisterClientScriptBlock(this.GetType(), "setIDFollow", csText.ToString(),true);
            }
        }

        protected void btnSync_Click(object sender, EventArgs e)
        {
            try
            {
                lblMsg.Text = "";
                SqlConnection connect_src = new SqlConnection(ConfigurationManager.ConnectionStrings["connect_src"].ConnectionString);
                string sql = "SELECT * FROM " + ddlTable.SelectedValue;
                SqlCommand cmd = new SqlCommand(sql, connect_src);
                DataTable dt = getDataTable(cmd);
                if (dt == null)
                {
                    lblMsg.Text = lblMsg.Text + "null";
                    return;
                }

                SqlConnection connect_des = new SqlConnection(ConfigurationManager.ConnectionStrings["connect_des"].ConnectionString);
                using (var bulkCopy = new SqlBulkCopy(connect_des.ConnectionString, SqlBulkCopyOptions.KeepIdentity))
                {
                    // my DataTable column names match my SQL Column names, so I simply made this loop. However if your column names don't match, just pass in which datatable name matches the SQL column name in Column Mappings
                    foreach (DataColumn col in dt.Columns)
                    {
                        bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
                    }
                    bulkCopy.BulkCopyTimeout = 600;
                    bulkCopy.DestinationTableName = ddlTable.SelectedValue;
                    bulkCopy.WriteToServer(dt);
                }
                lblMsg.Text = lblMsg.Text + ddlTable.SelectedValue + " [" + dt.Rows.Count + "]";
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.ToString();
            }
        }

        public DataTable getDataTable(SqlCommand cmd)
        {
            try
            {
                using (DataSet ds = new DataSet())
                {
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        da.SelectCommand = cmd;
                        da.Fill(ds);
                        return ds.Tables[0];
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.ToString();
                return null;
            }
            finally
            {
                cmd.Connection.Close();
            }
        }

        protected void btnCheckConnect_Click(object sender, EventArgs e)
        {
            lblMsg.Text = "";
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect_src"].ConnectionString);
                string sql = "SELECT GETDATE()";
                SqlCommand cmd = new SqlCommand(sql, con);
                DataTable dt = getDataTable(cmd);
                if (dt.Rows.Count > 0)
                {
                    lblMsg.Text = lblMsg.Text + "<BR/>- connect_src: Connected!";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = lblMsg.Text + ex.ToString();
            }

            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect_des"].ConnectionString);
                string sql = "SELECT GETDATE()";
                SqlCommand cmd = new SqlCommand(sql, con);
                DataTable dt = getDataTable(cmd);
                if (dt.Rows.Count > 0)
                {
                    lblMsg.Text = lblMsg.Text + "<BR/>- connect_des: Connected!";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = lblMsg.Text + ex.ToString();
            }
        }
    }
}